package com.qlu.syq.controller.admin;

import java.util.HashMap;
import java.util.Map;

import com.qlu.syq.service.admin.FloorService;
import com.qlu.syq.entity.RoomType;
import com.qlu.syq.page.admin.Page;
import com.qlu.syq.service.RoomTypeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 房间类型管理后台控制器
 *
 * @author Administrator
 */
@RequestMapping("/admin/room_type")
@Controller
public class RoomTypeController {

    @Autowired
    private FloorService floorService;

    @Autowired
    private RoomTypeService roomTypeService;


    /**
     * 房间类型管理列表页面
     *
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model) {
        model.addObject("floorList", floorService.findAll(new HashMap<>()));
        model.setViewName("room_type/list");
        return model;
    }

    /**
     * 房间类型信息添加操作
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(RoomType roomType) {
        Map<String, String> ret = new HashMap<>();
        if (roomType == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的房间类型信息!");
            return ret;
        }
        if (StringUtils.isEmpty(roomType.getName())) {
            ret.put("type", "error");
            ret.put("msg", "房间类型名称不能为空!");
            return ret;
        }
        // 默认房间数等于可用房间数=0  具体数量通过添加对于房间空间
        roomType.setRoomNum(0);
        roomType.setAvilableNum(0);
        // 设置预定数0
        roomType.setBookNum(0);
        // 设置已入住数0
        roomType.setLivedNum(0);
        if (roomTypeService.add(roomType) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "添加失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "添加成功!");
        return ret;
    }

    /**
     * 房间类型信息编辑操作
     *
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(RoomType roomType) {
        Map<String, String> ret = new HashMap<>();
        if (roomType == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的房间类型信息!");
            return ret;
        }
        if (StringUtils.isEmpty(roomType.getName())) {
            ret.put("type", "error");
            ret.put("msg", "房间类型名称不能为空!");
            return ret;
        }
        RoomType existRoomType = roomTypeService.find(roomType.getId());
        if (existRoomType == null) {
            ret.put("type", "error");
            ret.put("msg", "未找到该数据!");
            return ret;
        }
//        int offset = roomType.getRoomNum() - existRoomType.getRoomNum();
//        roomType.setAvilableNum(existRoomType.getAvilableNum() + offset);
//        if (roomType.getAvilableNum() <= 0) {
//            roomType.setAvilableNum(0);//没有可用房间
//            roomType.setStatus(0);//房型已满
//            if (roomType.getAvilableNum() + existRoomType.getLivedNum() + existRoomType.getBookNum() > roomType.getRoomNum()) {
//                ret.put("type", "error");
//                ret.put("msg", "房间数设置不合理!");
//                return ret;
//            }
//        }
        if (roomTypeService.edit(roomType) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "修改失败，请联系管理员!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "修改成功!");
        return ret;
    }

    /**
     * 分页查询房间类型信息
     *
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "floorId", required = false) Integer floorId,
            Page page) {
        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("name", name);
        queryMap.put("status", status);
        queryMap.put("floorId", floorId);

        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", roomTypeService.findList(queryMap));
        ret.put("total", roomTypeService.getTotal(queryMap));
        return ret;
    }

    /**
     * 房间类型信息删除操作
     *
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(Long id) {
        Map<String, String> ret = new HashMap<>();
        if (id == null) {
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的信息!");
            return ret;
        }
        try {
            if (roomTypeService.delete(id) <= 0) {
                ret.put("type", "error");
                ret.put("msg", "删除失败，请联系管理员!");
                return ret;
            }
        } catch (Exception e) {
            // TODO: handle exception
            ret.put("type", "error");
            ret.put("msg", "该房间类型下存在房间信息，请先删除该房间类型下的所有房间信息!");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "删除成功!");
        return ret;
    }

    @RequestMapping(value = "/getListByFloorId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getListByFloorId(@RequestParam(name = "floorId") Integer floorId) {
        Map<String, Object> ret = new HashMap<>();

        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("floorId", floorId);

        ret.put("rows", roomTypeService.findList(queryMap));
        return ret;
    }

    @RequestMapping(value = "/getListAll", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getListAll() {
        Map<String, Object> ret = new HashMap<>();
        ret.put("rows", roomTypeService.findAll(new HashMap<>()));
        return ret;
    }
}
